import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';
import 'package:yt_devops/components/app_bar/app_bar_widget.dart';
import 'package:yt_devops/components/app_permission/permission_widget.dart';
import 'package:yt_devops/components/brn/brn_bottom_button_panel.dart';
import 'package:yt_devops/components/gaps.dart';
import 'package:yt_devops/components/log_utils.dart';
import 'package:yt_devops/components/yt_button.dart';
import 'package:yt_devops/components/yt_text_style.dart';
import 'package:yt_devops/pages/site/site_setting/siet_elec_price_setting/add_mains_electricity_price/views/item_tow_checkbox_widget.dart';
import 'package:yt_devops/resources/str_res_keys.dart';
import 'package:yt_devops/styles/styles.dart';
import 'package:yt_devops/utils/constants/permission_page_constant.dart';
import 'package:yt_devops/utils/func_utils.dart';
import '../site_setting_base_info/widgets/item_widget.dart';
import 'video_monitor_setting_controller.dart';

class VideoMonitorSettingPage extends GetView<VideoMonitorSettingController> {
  const VideoMonitorSettingPage({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    Log.d('isSsoJump===' + controller.isSsoJump.toString());

    return GetBuilder<VideoMonitorSettingController>(
      builder: (controller) => Scaffold(
        backgroundColor: Colour.backgroundColor,
        resizeToAvoidBottomInset: false,
        appBar: AppBarWidget.withRightActions(ResKey.videoMonitorSetting.tr),
        body: ListView(padding: EdgeInsets.zero, children: [
          Container(
            margin: const EdgeInsets.only(top: 10, left: 16, right: 16),
            decoration: BoxDecoration(
              borderRadius: BorderRadius.circular(8),
              color: Colors.white,
            ),
            clipBehavior: Clip.hardEdge,
            padding: EdgeInsets.fromLTRB(16.w, 0, 16.w, 0),
            height: 50,
            child: Row(
              mainAxisAlignment: MainAxisAlignment.spaceBetween,
              children: [
                Text(ResKey.siteSettingObserverPhotovoltaicObserverStatus.tr,
                    style:
                        ytTextStyle(fontSize: 16, fontWeight: FontWeight.w500)),
                CupertinoSwitch(
                    activeColor: Colour.primaryColor,
                    value: controller.isOpen,
                    onChanged: FuncUtils.isPassAuthority([
                      PermissionConstant.homeSiteManageSettingVideoSettingSave
                    ])
                        ? (isOpen) {
                            Log.d('isOpen==' + isOpen.toString());
                            controller.isOpen = isOpen;
                            controller.checkoutCanCommit();
                            controller.update();
                          }
                        : null)
              ],
            ),
          ),
          Gaps.vGap10,
          Container(
              clipBehavior: Clip.hardEdge,
              margin: const EdgeInsets.only(left: 16, right: 16),
              decoration: BoxDecoration(
                borderRadius: BorderRadius.circular(8),
                // color: Colors.white,
              ),
              child: Column(
                children: [
                  Visibility(
                      visible: controller.isOpen,
                      child: Column(children: [
                        Container(
                          height: 53,
                          padding: const EdgeInsets.only(
                              left: 16, top: 10, bottom: 10),
                          color: Colors.white,
                          alignment: Alignment.centerLeft,
                          child: Text(
                            ResKey.webRedirection.tr,
                            style: ytTextStyle(
                                fontWeight: FontWeight.w500, fontSize: 16),
                          ),
                        ),
                        Container(
                          height: 0.5,
                          color: Colour.dividerColor,
                          margin: EdgeInsets.only(left: 16.w, right: 16.w),
                        ),
                        ItemTowCheckboxWidget(
                          ResKey.jumpMethod.tr,
                          ResKey.directJump.tr,
                          ResKey.sSOJump.tr,
                          controller.isSsoJump ? 0 : 1,
                          (index) {
                            Log.d('index===' + index.toString());
                            if (index == 0) {
                              controller.isSsoJump = true;
                            } else {
                              controller.isSsoJump = false;
                            }
                            controller.update();
                          },
                          isShowDivider: true,
                        ),
                        if (controller.isSsoJump)
                          Column(
                            children: [
                              ItemWidget(
                                ResKey.producer.tr,
                                controller.factoryController,
                                hintString: ResKey.pleSelect.tr,
                                isCanEdit: false,
                                isShowRight: true,
                                titleColor: Colour.otherTextColor,
                                clickCallBack: () {
                                  controller.chooseFactory();
                                },
                                inputTextColor: Colour.mainTextColor,
                              ),
                              _getItem(
                                'URL',
                                controller.urlController,
                              ),
                              _getItem(
                                ResKey.userID.tr,
                                controller.userIdController,
                              ),
                              _getItem(
                                'App Key',
                                controller.appKeyController,
                              ),
                              _getItem(
                                'Secret Key',
                                controller.secretKeyController,
                              ),
                            ],
                          ),
                        if (!controller.isSsoJump)
                          _getItem(
                            'URL',
                            controller.directUrlController,
                          )
                      ])),
                ],
              )),
          Gaps.vGap10,
          Container(
              clipBehavior: Clip.hardEdge,
              margin: const EdgeInsets.only(left: 16, right: 16),
              decoration: BoxDecoration(
                borderRadius: BorderRadius.circular(8),
                // color: Colors.white,
              ),
              child: Column(children: [
                Container(
                  height: 53,
                  padding: EdgeInsets.only(left: 16, top: 10, bottom: 10),
                  color: Colors.white,
                  alignment: Alignment.centerLeft,
                  child: Text(
                    ResKey.appRedirection.tr,
                    style:
                        ytTextStyle(fontWeight: FontWeight.w500, fontSize: 16),
                  ),
                ),
                Container(
                  height: 0.5,
                  color: Colour.dividerColor,
                  margin: EdgeInsets.only(left: 16.w, right: 16.w),
                ),
                ItemWidget(
                  ResKey.androidAppName.tr,
                  controller.androidController,
                  hintString: ResKey.pleSelect.tr,
                  isCanEdit: false,
                  isShowRight: true,
                  titleColor: Colour.otherTextColor,
                  inputTextColor: Colour.mainTextColor,
                  clickCallBack: () {
                    controller.chooseApp(1);
                  },
                ),
                ItemWidget(
                  ResKey.iOSAppName.tr,
                  controller.iosController,
                  hintString: ResKey.pleSelect.tr,
                  isCanEdit: false,
                  isShowRight: true,
                  inputTextColor: Colour.mainTextColor,
                  titleColor: Colour.otherTextColor,
                  clickCallBack: () {
                    controller.chooseApp(2);
                  },
                ),
              ]))
        ]),
        bottomNavigationBar: Container(
          color: Colour.white,
          padding: EdgeInsets.only(
              bottom: FuncUtils.isPassAuthority([
            PermissionConstant.homeSiteManageSettingVideoSettingSave
          ])
                  ? 25
                  : 0),
          child: PermissionWidget(
              permissionStringList: const [
                PermissionConstant.homeSiteManageSettingVideoSettingSave
              ],
              child: YtBrnBottomButtonPanel(
                  enableMainButton: controller.isCanSubmit,
                  mainButtonName: ResKey.save.tr,
                  mainButtonOnTap: () => controller.save(),
                  secondaryButtonName: ResKey.cancel.tr,
                  enableSecondaryButton: true,
                  secondaryButtonBorder: Border.all(color: Colour.dividerColor),
                  secondaryButtonOnTap: () => controller.cancel())),
        ),
      ),
    );
  }

  Widget _getItem(
    String title,
    TextEditingController textEditingController,
  ) {
    return ItemWidget(
      title,
      textEditingController,
      hintString: ResKey.plsInput.tr,
      isCanEdit: true,
      isShowRight: false,
      titleColor: Colour.otherTextColor,
      inputTextColor: Colour.mainTextColor,
      textChangeCallBack: (str) {
        Log.d('str==' + controller.urlController.text.toString());

        controller.checkoutCanCommit();
      },
    );
  }
}
